package com.ysh.messagewall.controller;

import com.ysh.messagewall.model.MessageInfo;
import com.ysh.messagewall.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RequestMapping("/message")
@RestController
public class MessageController {
    @Autowired
    private MessageService messageService;

    //private List<MessageInfo> messageInfoList = new ArrayList<>();

    @PostMapping(value = "/publish",produces = "application/json")
    //@PostMapping(value = "/publish")
    public String publish(@RequestBody MessageInfo info){
        if(!StringUtils.hasLength(info.getMessage())
                ||!StringUtils.hasLength(info.getTo())
                ||!StringUtils.hasLength(info.getFrom())){
            return "{\"OK\": 0}";
        }
        messageService.addMessage(info);
        //messageInfoList.add(info);
        return "{\"OK\": 1}";
    }
    @RequestMapping("/getList")
    public List<MessageInfo> getList() {
        /*return messageInfoList;*/
        return messageService.queryAll();
    }
}
